package org.stragma.statistics

import aterm._

object StrategoMeasurements {
	/*
	 * Method counts the number of terms with a specific name in an AST.
	 * This method uses recursion to traverse the tree
	 */
	def countTerms(startTerm : ATerm, termName : String) : Int = {
		var noOfStrategies = 0
		
		startTerm.getType match {
			case ATerm.APPL =>
			
				val appl = startTerm.asInstanceOf[ATermAppl]
				appl.getAFun.getName match {
					case boundName if(boundName == termName) =>
						noOfStrategies = noOfStrategies + 1
					case _ =>
				}
				
				for(i <- 0 until appl.getArity) {
					noOfStrategies = noOfStrategies + countTerms(appl.getArgument(i),termName)
				}
				
			case ATerm.LIST =>
			
				var list = startTerm.asInstanceOf[ATermList];
				while(!list.isEmpty) {
					noOfStrategies = noOfStrategies + countTerms(list.getFirst,termName)
					list = list.getNext
				}
			
			case _ => // Do Nothing
		}
		
		noOfStrategies
	}
}